package ca.usherbrooke.application.astar.location;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.geom.Rectangle;

import ca.usherbrooke.application.astar.Road;
import ca.usherbrooke.model.entity.IEntityModel;
import ca.usherbrooke.model.world.IWorldModel;
import ca.usherbrooke.tools.position.Vector2D;

public class CompleteLocation implements ILocation {
	private static final long serialVersionUID = 1L;

	private final IEntityModel entity;

	private Vector2D position;
	private final Float speedFactor;
	private ArrayList<Road> roads;
	private ArrayList<ILocation> neighbor;

	private final Boolean isWall;
	private Boolean isEntity;

	public CompleteLocation(Vector2D position, IWorldModel world,
			IEntityModel entity, Float speedFactor, Boolean isWall) {
		this.position = position;
		this.speedFactor = speedFactor;
		this.entity = entity;
		this.isWall = isWall;
		this.isEntity = !verifyFree(world);

		this.roads = new ArrayList<Road>();
		this.neighbor = new ArrayList<ILocation>();
	}

	private boolean verifyFree(IWorldModel world) {
		float width = this.entity.getSize().getWidth();
		float height = this.entity.getSize().getHeight();

		float widthCollid = this.entity.getSize().getWidth() * 2;
		float heightCollid = this.entity.getSize().getHeight() * 2;

		Rectangle rect = new Rectangle(0, 0, widthCollid, heightCollid);

		Boolean notWall = true;
		Vector2D test = position.clone();
		test.addX(-(width / 2));
		test.addY(-(height / 2));
		rect.setLocation(test.getX().floatValue(), test.getY().floatValue());
		List<IEntityModel> model = world.getEntities(rect);
		for (IEntityModel entityModel : model) {
			notWall = entity.equals(entityModel) && notWall;
		}

		return notWall;
	}

	public void revalidate(IWorldModel world) {
		isEntity = !verifyFree(world);
	}

	/*
	 * GETTER ET SETTER
	 */
	@Override
	public Vector2D getPosition() {
		return position;
	}

	public void setPosition(Vector2D position) {
		this.position = position;
	}

	@Override
	public ArrayList<Road> getRoads() {
		return roads;
	}

	@Override
	public void setRoads(ArrayList<Road> roads) {
		this.roads = roads;
	}

	@Override
	public Float getSpeedFactor() {
		return this.speedFactor;
	}

	@Override
	public String toString() {
		return position.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((position == null) ? 0 : position.hashCode());
		result = prime * result + ((roads == null) ? 0 : roads.hashCode());
		result = prime * result
				+ ((speedFactor == null) ? 0 : speedFactor.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CompleteLocation other = (CompleteLocation) obj;
		if (position == null) {
			if (other.position != null)
				return false;
		} else if (!position.equals(other.position))
			return false;
		if (roads == null) {
			if (other.roads != null)
				return false;
		} else if (!roads.equals(other.roads))
			return false;
		if (speedFactor == null) {
			if (other.speedFactor != null)
				return false;
		} else if (!speedFactor.equals(other.speedFactor))
			return false;
		return true;
	}

	@Override
	public Boolean isWall() {
		return isWall;
	}

	@Override
	public Boolean isEntity() {
		return isEntity;
	}

	@Override
	public Float getDistance(ILocation location) {
		Float distance = this.getPosition().getDistance(location.getPosition());
		return distance;
	}

	@Override
	public void setNeighbor(ArrayList<ILocation> neighbor) {
		this.neighbor = neighbor;
	}

	@Override
	public ArrayList<ILocation> getNeighbor() {
		return this.neighbor;
	}
}
